using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CustomLitJson;
using System.Threading.Tasks;
using Sirenix.OdinInspector;
using System;

public class CloundSave : StuffObject<CloundSave>
{
    public Dictionary<string, string> stageData;

    public async Task LoadAsync()
    {
        var gameId = GameManifestManager.Stuff.Get("gameId");
        var uid = CwManager.Stuff.data.uid;
        var finalUid = $"{gameId}-{uid}";
        var json = await CloundSaveApi.RequestAsync(finalUid);
        try
        {
            var stagedData = JsonMapper.Instance.ToObject<Dictionary<string, string>>(json);
            if(stagedData == null)
            {
                stagedData = new Dictionary<string, string>();
            }
            this.stageData = stagedData;
        }
        catch(Exception e)
        {
            Debug.Log("[CloundSave] clound save data error, use new");
            stageData = new Dictionary<string, string>();
        }
    }

    public async Task SaveAsync()
    {
        var gameId = GameManifestManager.Stuff.Get("gameId");
        var uid = CwManager.Stuff.data.uid;
        var finalUid = $"{gameId}-{uid}";
        var json = JsonMapper.Instance.ToJson(stageData);
        var b = await CloundSaveApi.SaveAsync(finalUid, json);
        if(!b)
        {
            Debug.LogError("[CloundSave] save error, skip");
        }
    }
}
